package com.wyl.aop;


import javax.servlet.http.HttpServletRequest;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.wyl.utils.ConvertTools;


@Aspect
@Component
public class WebLogAspect {

	private Logger log = LoggerFactory.getLogger(getClass());
	
	@Pointcut("execution(public * com.wyl.controller..*.*(..))")
	public void WebLog() {}
	
	@Before("WebLog()")
	public void doBefore(JoinPoint point)throws Throwable {
		ServletRequestAttributes attributes  = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
		HttpServletRequest request = attributes.getRequest();
		log.info(String.format("URL:\t%s,%s",
				request.getMethod(),request.getRequestURL().toString()));
		
		log.info("Method:\t"+point.getSignature().getDeclaringTypeName()
				+"."+point.getSignature().getName());
		
		log.info("Args:\t"+ConvertTools.mapToList((request.getParameterMap())).toString());
		
	}
	
	@AfterReturning(returning="ret",pointcut="WebLog()")
	public void afterReturned(Object ret) {
		log.info(String.format("Return:\t%s",ret.toString()));
	}
}
